/**
 * 全站http配置
 *
 * header参数说明
 * serialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from 'axios'
import store from '@/store/'
import { router } from '@/router/index'
import { baseUrl } from '@/config/env'
import { serialize } from '@/utils/common'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
import website from '@/const/website'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

axios.defaults.timeout = 10000
//返回其他状态吗
axios.defaults.validateStatus = function(status) {
    return status >= 200 && status <= 500 // 默认的
}
//跨域请求，允许保存cookie
axios.defaults.withCredentials = true
// NProgress Configuration
NProgress.configure({
    showSpinner: false
})

/**
 * HTTPrequest拦截
 */
axios.interceptors.request.use(config => {
    // start progress bar
    NProgress.start()
    const arrRouter = window.location.href.split('/#/')
    if(arrRouter.length == 2)
        config.headers['Router'] = arrRouter[1]
    if(config.url.indexOf('#/') == 0) {
        config.url = config.url.substring(1, config.url.length)
    } else {
        config.url = baseUrl + config.url
    }
    const isToken = (config.data || {}).isToken === false
    if (getToken() && !isToken) {
        // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
        config.headers['Authorization'] = getToken()
    }
    //headers中配置serialize为true开启序列化
    if (config.methods === 'post' && config.headers.serialize) {
        config.data = serialize(config.data)
        delete config.data.serialize
    }
    return config
}, error => {
    return Promise.reject(error)
})

/**
 * HTTPresponse拦截
 */
axios.interceptors.response.use(res => {
    NProgress.done()
    const status = Number(res.status) || 200
    const statusWhiteList = website.statusWhiteList || []
    const message = res.data.message || '未知错误'
    //如果请求为200则放过，否者默认统一处理,或者在website中配置statusWhiteList白名单自行处理
    if (status !== 200 && !statusWhiteList.includes(status)) {
        Message({
            message: message,
            type: 'error'
        })

        return Promise.reject(new Error(message))
    }
    //如果是401则跳转到登录页面
    if (status === 401) store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }))
    // 如果是白名单类型放入catch自行处理
    if (status !== 200) return Promise.reject(res)

    return res
}, error => {
    console.log(error)
    NProgress.done()

    return Promise.reject(new Error(error))
})

export default axios